package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemDictionaryQueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    @RequirePermission(name = "数据字典查询",expression = "systemDictionary:list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQueryObject qo) {
        // 分页和过滤
        model.addAttribute("pageInfo", systemDictionaryService.query(qo));
        return "systemDictionary/list";
    }

    // 查询数据 以树结构形式
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map> treeData() {
        return systemDictionaryService.queryTreeData();
    } // 查询数据 以树结构形式

    // 通过父id查询 业务子级 => 二级联动
    @RequestMapping("/queryByParentId")
    @ResponseBody
    public List<SystemDictionary> queryByParentId(Long parentId) {
        return systemDictionaryService.queryByParentId(parentId);
    }


    @RequestMapping("/delete")
    @RequirePermission(name = "数据字典删除",expression = "systemDictionary:delete")
    public String delete(Long id) {
        if (id != null) {
            systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list";
    }

   /* @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "部门编辑", expression = "systemDictionary:saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary) {
        if (systemDictionary.getId() != null) {
            systemDictionaryService.update(systemDictionary);
        } else {
            systemDictionaryService.insert(systemDictionary);
        }
        return "redirect:/systemDictionary/list";
    }*/

   // 异步的方式
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequirePermission(name = "数据字典编辑", expression = "systemDictionary:saveOrUpdate")
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {
        if (systemDictionary.getId() != null) {
            systemDictionaryService.update(systemDictionary);
        } else {
            systemDictionaryService.insert(systemDictionary);
        }
        return new JsonResult(true, "添加/保存成功");
    }
}
